package me.zhanshi123.vipsystem;

import cn.nukkit.plugin.PluginBase;
import me.zhanshi123.vipsystem.api.VipSystemAPI;
import me.zhanshi123.vipsystem.command.CommandHandler;
import me.zhanshi123.vipsystem.command.Commands;
import me.zhanshi123.vipsystem.custom.CustomManager;
import me.zhanshi123.vipsystem.customcommand.CustomCommandManager;
import me.zhanshi123.vipsystem.data.Cache;
import me.zhanshi123.vipsystem.data.Database;
import me.zhanshi123.vipsystem.feature.FeatureManager;
import me.zhanshi123.vipsystem.feature.vipjoin.VipJoin;
import me.zhanshi123.vipsystem.listener.PlayerListener;
import me.zhanshi123.vipsystem.manager.ConfigManager;
import me.zhanshi123.vipsystem.manager.MessageManager;
import me.zhanshi123.vipsystem.permission.PermissionHandler;
import me.zhanshi123.vipsystem.permission.impl.LuckPerms;
import me.zhanshi123.vipsystem.permission.impl.MultiPass;
import me.zhanshi123.vipsystem.script.ScriptManager;
import me.zhanshi123.vipsystem.task.CheckAllTask;

public final class Main extends PluginBase {
    private static Main instance;
    private static ConfigManager configManager;
    private static Database database;
    private static CommandHandler commandHandler;
    private static Cache cache;
    private static CustomCommandManager customCommandManager;
    private static CustomManager customManager;
    private static ScriptManager scriptManager;
    private static FeatureManager featureManager;
    private static PermissionHandler permissionHandler;

    public static PermissionHandler getPermissionHandler() {
        return permissionHandler;
    }

    public static Main getInstance() {
        return instance;
    }

    public static ConfigManager getConfigManager() {
        return configManager;
    }


    public static Database getDataBase() {
        return database;
    }


    public static CommandHandler getCommandHandler() {
        return commandHandler;
    }

    public static Cache getCache() {
        return cache;
    }

    public static CustomCommandManager getCustomCommandManager() {
        return customCommandManager;
    }

    public static CustomManager getCustomManager() {
        return customManager;
    }

    public static ScriptManager getScriptManager() {
        return scriptManager;
    }

    public static FeatureManager getFeatureManager() {
        return featureManager;
    }

    @Override
    public void onEnable() {
        instance = this;
        customCommandManager = new CustomCommandManager();
        configManager = new ConfigManager();
        MessageManager.init();
        database = new Database();
        if (!database.isAvailable()) {
            getServer().getConsoleSender().sendMessage(MessageManager.getString("fatalError"));
            setEnabled(false);
            return;
        }
        database.prepare();
        commandHandler = new CommandHandler();
        scriptManager = new ScriptManager();
        customManager = new CustomManager();
        getServer().getCommandMap().register("vipsys", new Commands());
        cache = new Cache();
        featureManager = new FeatureManager();
        loadVipFeature();
        getServer().getScheduler().scheduleRepeatingTask(new CheckAllTask(), 20 * 60, true);
        getServer().getPluginManager().registerEvents(new PlayerListener(), instance);
        VipSystemAPI.getInstance().getOnlinePlayers().forEach(player -> cache.cache(player));
        if (getServer().getPluginManager().getPlugin("Multipass") != null) {
            permissionHandler = new MultiPass();
        } else if (getServer().getPluginManager().getPlugin("LuckPerms") != null) {
            permissionHandler = new LuckPerms();
        } else {
            permissionHandler = null;
            getLogger().warning("No permission handler found!Please install LuckPerms or Multipass");
            setEnabled(false);
        }
    }

    @Override
    public void onDisable() {
        VipSystemAPI.getInstance().getOnlinePlayers().forEach(player -> Main.getCache().deCache(player));
        getServer().getScheduler().cancelTask(Main.getInstance());
        database.release();
    }

    private void loadVipFeature() {
        new VipJoin().register();
    }

    public void reload() {
        VipSystemAPI.getInstance().getOnlinePlayers().forEach(player -> Main.getCache().deCache(player));
        getServer().getScheduler().cancelTask(Main.getInstance());
        database.release();
        //disable & enable
        customCommandManager = new CustomCommandManager();
        configManager = new ConfigManager();
        MessageManager.init();
        database = new Database();
        if (!database.isAvailable()) {
            getServer().getConsoleSender().sendMessage(MessageManager.getString("fatalError"));
            setEnabled(false);
            return;
        }
        database.prepare();
        commandHandler = new CommandHandler();
        cache = new Cache();
        scriptManager = new ScriptManager();
        customManager = new CustomManager();
        getServer().getScheduler().scheduleRepeatingTask(new CheckAllTask(), 20 * 60, true);
        VipSystemAPI.getInstance().getOnlinePlayers().forEach(player -> cache.cache(player));
    }

}
